
def isPrime(num):
    import math
    for i in range(2,int(math.sqrt(num))+1):
        if num % i == 0:
            return False
    return True

def primeGenerator():
    yield 2
    yield 3
    start = 3
    while 1:
        start += 2
        if isPrime( start ):
            yield start
def main():

    limit = 2*(10**6)
    tot = 0
    import time
    t1 = time.time()
    for i in primeGenerator():
        if i > limit:
            break
        tot += i
    t2 = time.time()
    print 'the result is:',tot
    print 'take time %f (s)' % (t2-t1)
    primes = [2,3]
    tot = sum(primes)
    for i in range(5,limit,2):
        flag = True
        for j in range(len(primes)):
            if i % primes[j] == 0:
                flag = False
                break
        if flag:
            primes.append(i)
            tot += i
    t3 = time.time()
    print tot
    print 'take time %f (s)' % (t2-t1)
main()
